﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
//using AmCharts.Windows.Line;
using SpectrumAnalyser.Controls.Wpf.ViewModels;
using SpectrumAnalyser.ModelObjects;
using SpectrumAnalyser.ModelObjects.Enum;
using SpectrumAnalyser.ModelObjects.Interfaces;

namespace SpectrumAnalyser.Shell.ViewModels
{
    public class SpectrumFileViewModel : DependencyObject, ISpectrumFile
    {
        public SpectrumFileViewModel()
        {
            _dataPoints = new List<IDataPoint>();
            MatchedPeakElementNames = new List<string>();
            Peaks = new ObservableCollection<Peak>();
        }

        public SpectrumFileViewModel(SpectrumFile spectrumFile)
        {
            MatchedPeakElementNames = spectrumFile.MatchedElementNames ?? new List<string>();
            PeakValue = spectrumFile.PeakValue;
            FileName = spectrumFile.FileName;
            WavelengthMeasure = spectrumFile.WavelengthMeasure;
            Peaks = new ObservableCollection<Peak>(spectrumFile.Peaks??new List<Peak>());
            _dataPoints = new List<IDataPoint>();

            foreach (var spectrumDataPoint in spectrumFile.DataPoints)
            {
                DataPoints.Add(new SpectrumDataPointViewModel
                    {
                        SpectrumPoint = new SpectrumPoint(spectrumDataPoint.SpectrumPoint.X, spectrumDataPoint.SpectrumPoint.Y),
                        IsChecked = true
                    });
            }
        }

        public SpectrumFile ToSpectrumFile()
        {
            return new SpectrumFile
                {
                    MatchedElementNames = MatchedPeakElementNames,
                    PeakValue = PeakValue,
                    FileName = FileName,
                    DataPoints =
                        DataPoints == null
                            ? new List<IDataPoint>()
                            : DataPoints.Cast<SpectrumDataPointViewModel>().Select(x => new SpectrumDataPoint()
                                {
                                    SpectrumPoint = x.SpectrumPoint
                                }).Cast<IDataPoint>().ToList(),
                    Peaks = Peaks == null ? new List<Peak>() : Peaks.ToList(),
                    WavelengthMeasure = WavelengthMeasure
                };
        }

        public static readonly DependencyProperty PeakValueProperty =
            DependencyProperty.Register("PeakValue", typeof (double), typeof (SpectrumFileViewModel),
                                        new PropertyMetadata(default(double)));

        public double PeakValue
        {
            get { return (double) GetValue(PeakValueProperty); }
            set { SetValue(PeakValueProperty, value); }
        }

        public static readonly DependencyProperty PeakElementNameProperty =
            DependencyProperty.Register("MatchedPeakElementNames", typeof(List<string>), typeof(SpectrumFileViewModel),
                                        new PropertyMetadata(default(List<string>)));

        public List<string> MatchedPeakElementNames
        {
            get { return (List<string>)GetValue(PeakElementNameProperty); }
            set { SetValue(PeakElementNameProperty, value); }
        }

        public string FileName { get; set; }

        public SpectrumFileViewModel Clone()
        {
            return new SpectrumFileViewModel
                       {
                           DataPoints = DataPoints.ToList(),
                           FileName = FileName,
                           MatchedPeakElementNames = MatchedPeakElementNames,
                           PeakValue = PeakValue,
                           WavelengthMeasure = WavelengthMeasure
                       };
        }

        private IList<IDataPoint> _dataPoints { get; set; }

        public IList<IDataPoint> DataPoints
        {
            get { return _dataPoints; }
            set
            {
                if (_dataPoints == null)
                {
                    _dataPoints = new List<IDataPoint>();
                }
                foreach (var dataPoint in value)
                {
                    _dataPoints.Add(new SpectrumDataPointViewModel() {SpectrumPoint = dataPoint.SpectrumPoint});
                }
            }
        }

        public WavelengthMeasure WavelengthMeasure { get; set; }

        #region SpectrumViewModel Fields

        public ObservableCollection<Peak> Peaks { get; set; }
        public ObservableCollection<SpectrumDataPointViewModel> Wavelet { get; set; }

        #endregion
    }
}